package org.jeecg.modules.thirdpartyapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.aspect.ThirdPartyEntityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.JsonUtils;
import org.jeecg.modules.syncdata.entity.SyncLog;
import org.jeecg.modules.syncdata.entity.SyncResult;
import org.jeecg.modules.syncdata.service.SyncLogService;
import org.jeecg.modules.syncdata.service.SyncResultService;
import org.jeecg.modules.thirdpartyapi.dao.PullDao;
import org.jeecg.modules.thirdpartyapi.dto.PullDto;
import org.jeecg.modules.thirdpartyapi.mapper.CommonMapper;
import org.jeecg.modules.thirdpartyapi.service.BgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * bg服务实现类
 *
 * @author zhanglei01
 * @date 2022/03/08
 */
@Slf4j
@Service
public class BgServiceImpl implements BgService {

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private SyncLogService syncLogService;

    @Autowired
    private SyncResultService syncResultService;

    @Override
    public IPage<Map<String, Object>> pullDataByUpdate(TableInfo tableInfo, PullDto dto) {
        PullDao pullDao = new PullDao();
        //判断是否有忽略返回字段
        String allSqlSelect = ThirdPartyEntityUtils.validateIgnore(tableInfo);
        if (StringUtils.isBlank(allSqlSelect)) {
            throw new JeecgBootException("获取信息异常！");
        }
        pullDao.setAllSqlSelect(allSqlSelect);
        pullDao.setTableName(tableInfo.getTableName());
        pullDao.setStartTime(dto.getStartTime());
        pullDao.setEndTime(dto.getEndTime());
        Page<Object> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        IPage<Map<String, Object>> mapIPage = null;
        try {
            mapIPage = commonMapper.pullDataByUpdate(page, pullDao);
        } catch (Exception e) {
            log.error("获取表{}，数据异常", tableInfo.getTableName(), e);
            throw new JeecgBootException("获取数据异常！");
        }
        Class<?> entityType = tableInfo.getEntityType();
        List list = JSONArray.parseArray(JSON.toJSONString(mapIPage.getRecords()), entityType);
        if (dto.getIsSnakeCase()) {
            //转大写蛇形命名
            list = JsonUtils.list2SnakeCase(list);
        }
        mapIPage.setRecords(list);
        return mapIPage;
    }

    @Override
    public Boolean saveSyncLog(List<SyncLog> syncLogs) {
        return syncLogService.saveOrUpdateBatchByMultiId(syncLogs);
    }

    @Override
    public Boolean saveSyncResult(List<SyncResult> syncResults) {
        return syncResultService.saveOrUpdateBatch(syncResults);
    }

}
